import calendar
from datetime import time

from django.http import HttpResponse
from django.shortcuts import render_to_response, render
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from unicom.data_collect.data_collect import DataCollectCenter




@method_decorator(csrf_exempt, name='dispatch')
class DataCollectService(DataCollectCenter):
    def redirect_login(self):
        """
        转入登录页面
        :return:
        """
        return render(self, "login.html")

    def data_collect(self):
        """
        采集用户信息，手机号、服务密码等
        :return:
        """
        user_info = {}
        if 'mobile' in self.POST and 'password' in self.POST:
            user_info['mobile'] = self.POST['mobile']
            user_info['password'] = self.POST['password']
            login_response= DataCollectService.login_unicom(user_info['mobile'], user_info['password'])
            #根据response处理 1.提交后 提示 密码错误，重新输入密码或重置密码 2.提交后 登陆成功,直接跳转到获取详细验证码页面，获取验证码
            #同步
            return render(self, "sendmessage.html")
    def redirect_forget_password(self):
        """
        跳转到忘记密码页面
        :return:
        """
        return render(self, "forgetpassword.html")

    def again_send_captcha(self):
        """
        重置密码时 重发短信验证码
        :return:
        """
        mobile = self.POST['mobile']
        captcha = self.get_image_captcha()
        idNO = self.POST["idNo"][0:6]
        self.send_captcha(mobile, captcha, idNO)
        msg =self.POST['msg']
        new_passord=self.POST['newpwd']
        #submit() 提交 并 登陆
        pass

    @staticmethod
    def login_unicom(mobile,password):
        """
        获取手机号与服务密码后 直接登录
        :param mobile: 手机号
        :param password: 服务密码
        :return:
        """
        return "登陆成功"

    def get_image_captcha(self):
        """
        修改密码时，破解图片验证码
        :return: 识别后的结果
        """
        return ""

    def send_captcha(self,mobile,captcha,idNo):
        """
        修改密码时 发送短信验证码
        :param mobile: 手机号
        :param captcha: 图片验证码
        :param idNo: 身份证后六位
        :return:
        """
        #发送短信验证码 并填入输入框
        return ""

    def get_report(self):
        """
        获取通话详情报告,并存放到数据库
        :return:
        """
        self.get_captcha()
        msg_captcha = self.POST['msg_captcha']
        self.get_detail()

    def get_captcha(self):
        """
        获取通话详情时，重发短信验证码
        :return:
        """


    def get_detail(self):
        """
        获取通话详情
        :return:
        """
        pass

    def getMonths(self):
        """
        :return: 本月之前五个月个月的月初月末日期以及本月的月初与当天日期
        列表格式  20170901-20170924
        """
        l = []
        year = time.localtime().tm_year
        mon = time.localtime().tm_mon
        day = time.localtime().tm_mday
        FORMAT = "%d%02d%02d-%d%02d%02d"
        for m in range(0, 6):
            mo = mon - m
            if m == 0:
                l.append(FORMAT % (year, mo, 1, year, mo, day))
            else:
                if mo == 0:
                    year -= 1
                if mo <= 0:
                    mo += 12
                d = calendar.monthrange(year, mo)
                l.append(FORMAT % (year, mo, 1, year, mo, d[1]))
        return l